<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 推荐模型
 *
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-29
 * Time: 上午10:37
 */
class Recommend_model extends MY_Model
{
    function __construct(){
        parent::__construct();
        $this->_table_name = 'recommend';
    }

    function display($recommend_id){
        $this->db->where('id',$recommend_id);
        $this->db->set('is_display',1);
        $this->db->update($this->_table_name);
        return $this->db->affected_rows()>0;
    }

    function close_display($recommend_id){
        $this->db->where('id',$recommend_id);
        $this->db->set('is_display',1);
        $this->db->update($this->_table_name);
        return $this->db->affected_rows()>0;
    }

    function select_recommend($data,$source,$fields=array()){
        //get_activities
        $this->db->select($this->_table_name.'.*');
        $select = '';
        if(empty($fields)){
            $select = $source.'.title,'.$source.'.cover';
        }else{
            foreach($fields as $v){
                $select .= $source.'.'.$v.',';
            }
            $select = rtrim($select,',');
        }
        $this->db->select($select);
        if(isset($data['source'])){
            $this->db->where('source',$data['source']);
        }
        if(isset($data['func']))
            $this->db->where('func',$data['func']);
        $this->db->where($this->_table_name.'.is_del',0);
        $this->db->join($source, $source.'.id = '.$this->_table_name.'.source_id','right');
        $query = $this->db->get($this->_table_name);
        if($query->num_rows()>0){
            return $query->result_array();
        }
        return null;
    }

    function find_one($id){
        $this->db->where('id',$id);
        $this->db->limit(1);
        $query = $this->db->get($this->_table_name);
        if($query->num_rows()==1)
            return $query->result_array()[0];
        return null;
    }
}